#include "objectmanager.h"
#include "object.h"

namespace ShCo
{
class ObjectManagerImpl : public Impl<ObjectManager>
{
public:
    void init();
    void exit();
    List<Object*>::Type _objs;
};

void ObjectManagerImpl::init()
{
}

void ObjectManagerImpl::exit()
{
    List<Object*>::Type::iterator it = _objs.begin();
    for (; it != _objs.end(); ++it)
    {
        delete *it;
    }
    _objs.clear();
}

ObjectManager::ObjectManager()
{
    SH_I(ObjectManager);
    d->init();
}

ObjectManager::~ObjectManager()
{
    SH_E(ObjectManager);
    d->exit();
}

bool ObjectManager::append(Object* obj)
{
    SH_D(ObjectManager);
    if (!obj)
        return false;
    List<Object*>::Type::iterator it = d->_objs.begin();
    for (; it != d->_objs.end(); ++it)
    {
        if (*it == obj)
            break;
    }
    if (it == d->_objs.end())
        d->_objs.push_back(obj);
    return true;
}

bool ObjectManager::remove(Object* obj)
{
    SH_D(ObjectManager);
    if (!obj)
        return false;
    List<Object*>::Type::iterator it = d->_objs.begin();
    for (; it != d->_objs.end(); ++it)
    {
        if (*it == obj)
        {
            d->_objs.erase(it);
            delete obj;
            return true;
        }
    }
 
    return false;
}

} // namespace ShCo
